/**
 * Copyright (C) 2013 Inera AB (http://www.inera.se)
 *
 * This file is part of Inera Certificate Proxy (http://code.google.com/p/inera-certificate-proxy).
 *
 * Inera Certificate Proxy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Inera Certificate Proxy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package se.inera.certificate.proxy.utils;

import com.google.common.base.Throwables;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.net.URL;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.commons.lang.StringUtils.endsWith;
import static org.apache.commons.lang.StringUtils.join;
import static org.apache.commons.lang.StringUtils.split;
import static org.apache.commons.lang.StringUtils.startsWith;

public final class PathUtil {

    private PathUtil() {
        // Empty
    }

    public static String joinFragments(String pathFragment1, String pathFragment2) {
        checkNotNull(pathFragment1, "Argument pathFragment1 is null!");
        checkNotNull(pathFragment2, "Argument pathFragment2 is null!");

        boolean fragment1startsWith = startsWith(pathFragment1, "/");
        boolean fragment2endsWith = endsWith(pathFragment2, "/");

        return slashOrEmpty(fragment1startsWith)
                +
                join(split(join(new String [] {pathFragment1, pathFragment2}, '/'), '/'), "/")
                +
                slashOrEmpty(fragment2endsWith);
    }

    private static String slashOrEmpty(boolean slash) {
        if (slash) {
            return "/";
        }
        return "";
    }

    public static String getPath(HttpServletRequest request) {
        checkNotNull(request, "Argument request is null!");
        checkNotNull(request.getRequestURL(), "Argument request.requestUrl is null!");

        String contextPath = StringUtils.defaultIfEmpty(request.getContextPath(), "");
        String result = null;
        try {
            result = new URL(request.getRequestURL().toString()).getPath();
            result = StringUtils.substringAfter(result, contextPath);
        } catch (MalformedURLException e) {
            Throwables.propagate(e);
        }
        return result;
    }

    public static String getRequestUrl(HttpServletRequest request) {
        checkNotNull(request, "Argument request is null!");
        checkNotNull(request.getRequestURL(), "Argument request.requestURL is null");

        return request.getRequestURL().toString();
    }

    public static String getFirstSegment(String path) {
        String[] parts = split(path, "/");
        return "/" + parts[0];
    }
}
